package com.company.cloud.coupon.modules.mgr.controller;

import java.util.List;
import java.util.Set;
import java.util.function.Function;

import com.company.cloud.common.bean.BaseClientResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.bean.LoginUserInfo;
import com.company.cloud.common.constant.BaseConstant.EnabledEnum;
import com.company.cloud.common.constant.BaseConstant.YNEnum;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.common.utils.StrSplitUtils;
import com.company.cloud.coupon.constant.CouponConstant.CouponUsableObjTypeEnum;
import com.company.cloud.coupon.constant.CouponErrorEnum;
import com.company.cloud.coupon.modules.base.entity.CouponConfigInfo;
import com.company.cloud.coupon.modules.base.service.CouponConfigInfoService;
import com.company.cloud.coupon.modules.cache.models.CouponConfigModel;
import com.company.cloud.coupon.modules.cache.utils.CouponChangeUtils;
import com.company.cloud.coupon.modules.mgr.controller.req.CouponConfigQuery4PageParam;
import com.company.cloud.coupon.modules.mgr.controller.req.CouponConfigSaveParam;
import com.company.cloud.coupon.modules.mgr.controller.req.CouponConfigUpDownParam;
import com.company.cloud.coupon.modules.mgr.controller.req.CouponConfigUpdateParam;
import com.company.cloud.coupon.modules.mgr.service.CouponConfigMgrService;
import com.company.cloud.product.modules.api.response.ProductSimple;
import com.company.cloud.product.modules.api.service.ProductRemoteService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 优惠券配置信息管理
 */
@Authc
@RestController
@RequestMapping("/mgr/op/couponConfig")
@Api(tags="OP_Mgr_优惠券配置信息管理")
public class CouponConfigMgrController {

	@Autowired
	private CouponConfigMgrService couponConfigMgrService;
	@Autowired
	private CouponConfigInfoService couponConfigInfoService;
	@Autowired
	private ProductRemoteService productRemoteService;
	
	
	@PostMapping("/query")
	@ApiOperation(value="查询", response=CouponConfigModel.class)
	public BaseResult query(@RequestBody CouponConfigQuery4PageParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		IPage<CouponConfigInfo> page = couponConfigInfoService.page(new Page<>(params.getPage(), params.getRows()), 
				new QueryWrapper<CouponConfigInfo>()
				.eq("rows_state", EnabledEnum.ENABLED.getCode())
				.eq("company_no", userInfo.getCompanyNo())
				.like(StringUtils.hasText(params.getTitle()), "title", params.getTitle())
				.eq(StringUtils.hasText(params.getCouponType()), "coupon_type", params.getCouponType())
				.eq(StringUtils.hasText(params.getIsUseLimit()), "is_use_limit", params.getIsUseLimit())
				.eq(StringUtils.hasText(params.getUsableObjType()), "usable_obj_type", params.getUsableObjType())
				.eq(StringUtils.hasText(params.getTotalNumType()), "total_num_type", params.getTotalNumType())
				.eq(StringUtils.hasText(params.getUpperLowerState()), "upper_lower_state", params.getUpperLowerState())
				);
		
		IPage<CouponConfigModel> data=page.convert(new Function<CouponConfigInfo, CouponConfigModel>() {
			@Override
			public CouponConfigModel apply(CouponConfigInfo entity) {
				return CouponChangeUtils.getCouponConfig(entity);
			}
		});
		
		return BaseResult.ok(data);
	}
	
	@PostMapping("/add")
	@ApiOperation(value="新增", response=BaseResult.class)
	public BaseResult add(@RequestBody CouponConfigSaveParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		couponConfigMgrService.save(params, userInfo);
		return BaseResult.ok();
	}
	
	@PostMapping("/edit")
	@ApiOperation(value="修改", response=BaseResult.class)
	public BaseResult edit(@RequestBody CouponConfigUpdateParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		couponConfigMgrService.update(params, userInfo);
		return BaseResult.ok();
	}
	
	@GetMapping("/get")
	@ApiOperation(value="获取", response=CouponConfigModel.class)
	public BaseResult get(@RequestParam("id")Integer id) {
		CouponConfigInfo couponConfig=couponConfigInfoService.getById(id);
		if(couponConfig==null || EnabledEnum.DISABLED.getCode().equals(couponConfig.getRowsState())) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		List<ProductSimple> products=null;
		if(CouponUsableObjTypeEnum.Appointed_UsableObj.getCode().equals(couponConfig.getUsableObjType())) {
			Set<String> productNos=StrSplitUtils.parse2set(couponConfig.getUsableObjNo(), String.class);
			BaseClientResult<List<ProductSimple>> productResp=productRemoteService.getProductSimplListByIds(productNos);
			products=BaseClientResult.getResultData(productResp);
		}
		CouponConfigModel model= CouponChangeUtils.getCouponConfig(couponConfig,products);
		return BaseResult.ok(model);
	}
	
	@GetMapping("/remove")
	@ApiOperation(value="删除", response=BaseResult.class)
	public BaseResult remove(@RequestParam("id")Integer id) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		CouponConfigInfo couponConfig=couponConfigInfoService.getById(id);
		if(couponConfig==null || EnabledEnum.DISABLED.getCode().equals(couponConfig.getRowsState())) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		if(YNEnum.YES.getCode().equals(couponConfig.getUpperLowerState())) {
			throw new BusinessException(CouponErrorEnum.Coupon_UpperLower);
		}
		couponConfig.setRowsState(EnabledEnum.DISABLED.getCode());
		couponConfig.addEditParam(userInfo);
		couponConfigInfoService.updateById(couponConfig);
		return BaseResult.ok();
	}
	
	
	@PostMapping("/upDown")
	@ApiOperation(value="上下架", response=BaseResult.class)
	public BaseResult upDown(@RequestBody CouponConfigUpDownParam params) {
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		CouponConfigInfo couponConfig=couponConfigInfoService.getById(params.getId());
		if(couponConfig==null || EnabledEnum.DISABLED.getCode().equals(couponConfig.getRowsState())) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		if(couponConfig.getUpperLowerState().equals(params.getUpperLowerState())) {
			return BaseResult.ok();
		}
		couponConfig.setUpperLowerState(params.getUpperLowerState());
		couponConfig.addEditParam(userInfo);
		couponConfigInfoService.updateById(couponConfig);
		return BaseResult.ok();
	}
	
}